package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.entity.vo.FindRuleByRoleVo;
import com.xueyi.aixihu.entity.vo.FindRuleFatherVo;
import com.xueyi.aixihu.entity.vo.TreeOut;
import com.xueyi.aixihu.mapper.LocalDtAuthRuleMapper;
import com.xueyi.aixihu.response.FindRuleByRoleResponse;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.DtAuthRuleService;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lvyi
 * @date : 2022/8/19
 * @Description : 权限表的service实现类
 */
@Service
public class DtAuthRuleServiceImpl implements DtAuthRuleService {

    @Autowired
    private LocalDtAuthRuleMapper localDtAuthRuleMapper;


    /**
     * 根据角色id查询其权限id
     *
     * @return
     */
    @Override
    public FindRuleByRoleResponse findRuleByRole(Integer id) {

        FindRuleByRoleResponse response = new FindRuleByRoleResponse();

        if (id < 1 || id == null) {
            response.setCode(SystemCode.QUERY_CHECK_ERROR);
            response.setMsg("角色id有误");
            return response;
        }

        FindRuleByRoleVo ruleByRole = localDtAuthRuleMapper.findRuleByRole(id);
        response.setFindRuleByRoleVo(ruleByRole);
        response.setCode(SystemCode.SUCCESS);
        response.setMsg("查询成功");
        return response;
    }


    /**
     * 查询出树状图样的权限名称与id
     *
     * @return
     */
    @Override
    public BaseResponse<List<FindRuleFatherVo>> findTreeOutRule() {
        // 查询出所有的父类名称以及id
        List<FindRuleFatherVo> ruleFather = localDtAuthRuleMapper.findRuleFather();
        for (FindRuleFatherVo findRuleFatherVo : ruleFather) {
            //根据父类id遍历查询其下的子类id以及名称
            List<TreeOut> ruleByFather = localDtAuthRuleMapper.findRuleByFather(findRuleFatherVo.getRuleId());
            findRuleFatherVo.setList(ruleByFather);
        }

        return BaseResponse.build(SystemCode.SUCCESS, "查询成功", ruleFather);
    }


    /**
     * 根据角色id关联权限
     *
     * @param id     角色id
     * @param ruleId 权限id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> updateRulesByRole(Integer id, String ruleId) {
        if (id == null || id < 1) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "角色id有误", Boolean.FALSE);
        } else if (StringUtils.isBlank(ruleId)) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "关联权限不可为空", Boolean.FALSE);
        }
        localDtAuthRuleMapper.updateRulesByRole(id, ruleId);

        LogUtil.buildUserLog(id.toString(), "【角色管理】用户" + DtUsersVo.findCurrentUser().getUsername() + "给id为：" + id + "的角色关联权限。", DtUsersVo.findCurrentUser(), null);
        return BaseResponse.build(SystemCode.SUCCESS, "关联成功", Boolean.TRUE);
    }
}
